<template>
  <el-dialog title="分配角色" :visible="showAssignRole" @close="btnCancel">
    <el-checkbox-group v-model="checkList">
      <el-checkbox v-for="item in roles" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <el-row slot="footer" type="flex" justify="center">
      <el-button type="primary" size="small" @click="btnScu">确认</el-button>
      <el-button type="info" size="small" @click="btnCancel">取消</el-button>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRoles } from '@/api/setting'
import { getUserDetail } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    showAssignRole: {
      type: Boolean,
      default: false
    },
    id: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      checkList: [],
      roles: []
    }
  },
  methods: {
    btnScu() {
      assignRoles({ id: this.id, roleIds: this.checkList }).then(() => {
        this.$message.success('更新成功')
      }, () => {
        this.getRoles()
        this.getCurrentRoles()
      })
      this.$emit('update:showAssignRole', false)
    //   this.$emit('closeRole')
    },
    btnCancel() {
      this.$emit('closeRole')
    },
    async getRoles() {
      try {
        const { rows } = await getRoles({ page: 1, pagesize: 10 })
        // console.log(rows)
        this.roles = rows
      } catch (error) {
        console.log(1)
      }
    },
    async getCurrentRoles() {
      try {
        const { roleIds } = await getUserDetail(this.id)
        console.log(roleIds)
        this.checkList = roleIds
      } catch (error) {
        console.log('getUserDetail接口报错')
      }
    }
  }
}
</script>

<style>

</style>
